package com.hailong.zyt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hailong.zyt.beans.CourseAll;
import com.hailong.zyt.beans.Homework;
import com.hailong.zyt.beans.HomeworkStudent;
import com.hailong.zyt.mapper.CourseAllMapper;
import com.hailong.zyt.mapper.HomeworkMapper;
import com.hailong.zyt.mapper.HomeworkStudentMapper;
import com.hailong.zyt.service.HomeworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hailong.zyt.service.HomeworkStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hailong
 * @since 2021-06-01
 */
@Service
public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, Homework> implements HomeworkService {

    private HomeworkMapper homeworkMapper;
    private CourseAllMapper courseAllMapper;
    private HomeworkStudentMapper homeworkStudentMapper;


    @Override
    public boolean getHomeworkByTeaId(Long teaId, Long couId, Model model) {
        Map<String, Object> map = new HashMap<>();
        map.put("cou_id", couId);
        map.put("tea_id", teaId);
        //查所有作业
        QueryWrapper<Homework> wrapper = new QueryWrapper<>();
        wrapper.allEq(map);
        List<Homework> homeworks = this.list(wrapper);


        //查这个课程所有学生数量
        QueryWrapper<CourseAll> courseAllQueryWrapper = new QueryWrapper<>();
        courseAllQueryWrapper.allEq(map);
        Integer student_all = courseAllMapper.selectCount(courseAllQueryWrapper);
        if(student_all == null) student_all = 0;

        //查提交了作业的学生
        for(Homework homework : homeworks) {
            Integer finished = homeworkStudentMapper.selectCount(new QueryWrapper<HomeworkStudent>().eq("hw_id", homework.getHwId()));
            if(finished == null) finished = 0;
            String rate = "" + finished + " / " + student_all;
            homework.setHwFinishedRate(rate);
        }

        model.addAttribute("homeworks", homeworks);
        model.addAttribute("couId", couId);
        return true;
    }

    @Override
    public boolean getHomeworkByStuId(Long stuId, Long couId, Model model) {
        //查所有作业
        QueryWrapper<Homework> wrapper = new QueryWrapper<>();
        wrapper.eq("cou_id", couId);
        List<Homework> homeworks = this.list(wrapper);

        //查询每个作业学生完成状况
        Map<String, Object> map = new HashMap();
        map.put("stu_id", stuId);
        for(Homework homework : homeworks) {
            map.put("hw_id", homework.getHwId());
            HomeworkStudent homeworkStudent = homeworkStudentMapper.selectOne(new QueryWrapper<HomeworkStudent>().allEq(map));

            LocalDateTime now = LocalDateTime.now();
            LocalDateTime s = homework.getHwStartTime(), e = homework.getHwEndTime();

            if(homeworkStudent == null){
                homework.setHwsMark(0);
                if(now.isBefore(s) || now.isAfter(e)) {
                    homework.setHwsState(3);
                }
                else homework.setHwsState(-1);
            }
            else {
                if(now.isBefore(s) || now.isAfter(e)) {
                    homeworkStudent.setHwsState(2);
                    homeworkStudentMapper.update(homeworkStudent, new QueryWrapper<HomeworkStudent>().allEq(map));
                }
                homework.setHwsState(homeworkStudent.getHwsState());
                Float mark = homeworkStudent.getHwsMark();
                if(mark == null) homework.setHwsMark(0);
                else homework.setHwsMark(mark);
            }
        }
        model.addAttribute("homeworks", homeworks);
        return false;
    }

    @Autowired
    public void setHomeworkMapper(HomeworkMapper homeworkMapper) {
        this.homeworkMapper = homeworkMapper;
    }

    @Autowired
    public void setCourseAllMapper(CourseAllMapper courseAllMapper) {
        this.courseAllMapper = courseAllMapper;
    }

    @Autowired
    public void setHomeworkStudentMapper(HomeworkStudentMapper homeworkStudentMapper) {
        this.homeworkStudentMapper = homeworkStudentMapper;
    }

}
